package com.kizuki.thread;

import org.junit.Test;

import java.util.concurrent.locks.LockSupport;

public class TestPark {

    /**
     * park和unpark 顺序无关
     */
    public static void main(String[] args) {
        Thread boyThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("boy: 我要吃鸡");
                LockSupport.park();
                System.out.println("boy: park1");
                LockSupport.park(); // 第二次会阻塞住，因为只有一个permit
                System.out.println("boy: park2");
                System.out.println("boy: 开始吃鸡了");
            }
        });

        Thread girlThread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("girl: 允许boy吃鸡");
                LockSupport.unpark(boyThread); // unpark两次，但是permit不会叠加
                LockSupport.unpark(boyThread);
            }
        });

        boyThread.start();
        girlThread.start();
    }

    /**
     * park的线程interrupt了会继续执行后面的语句
     */
    @Test
    public void testInterruptPark() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            LockSupport.park();
            System.out.println("t1运行中");
        });

        t1.start();
        Thread.sleep(2000);
        t1.interrupt();
    }

    /**
     * interrupt()：将调用该方法的对象所表示的线程标记一个停止标记，并不是真的停止该线程。
     * isInterrupted()：获取调用该方法的对象所表示的线程，不会清除线程的状态标记。是一个实例方法。
     * interrupted()：获取 当前线程 的中断状态，并且会清除线程的状态标记。是一个是静态方法。
     * 当线程呈wait()状态时，调用线程对象的interrupt方法会出现InterruptedException异常
     */
    @Test
    public void testInterrupt() throws InterruptedException {
        try {
            Thread myThread = new Thread(new MyThread());
            myThread.start();
            myThread.sleep(1000);
            myThread.interrupt();
        } catch (InterruptedException e) {
            System.out.println("main catch");
        }
        Thread.sleep(2000);
        System.out.println("main end");
    }

    /**
     * isInterrupted = true
     * interrupted = true
     * isInterrupted2 = false
     * interrupted2 = false
     */
    class MyThread implements Runnable {
        @Override
        public void run() {
            while(true) {
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("isInterrupted = " +  Thread.currentThread().isInterrupted());
                    System.out.println("interrupted = " +  Thread.interrupted());
                    System.out.println("isInterrupted2 = " +  Thread.currentThread().isInterrupted());
                    System.out.println("interrupted2 = " +  Thread.interrupted());
                    try {
                        throw new InterruptedException();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }
    }


}
